#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, Inc. All Rights Reserved.
// Assembly: Engine
// Code File Name: DatabaseManager.cs
// Code File Date: 01/20/2007
//
// Code File Info: Database Manager responsible for database utility functions.
//=============================================================================
/*
 * UPDATES
 * -------
 *                ShawnJ 5/4/2007 (0.9.0.x): Refactoring for OSS.
 */
#endregion Header

#region License
/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#endregion License

#region Includes
using System.Data;
using System.Data.SqlClient;
using System.ServiceProcess;
using System.Windows.Forms;
#endregion Includes

namespace WaveTech.GShield.Engine.Managers
{
  /// <summary>
  /// Primary manager for database activites
  /// </summary>
  public class DatabaseManager
  {
    #region Singleton
    static readonly DatabaseManager instance = new DatabaseManager();
    #endregion Singleton

    #region Instance Property
    public static DatabaseManager Instance
    {
      get
      {
        return instance;
      }
    }
    #endregion Instance Property

    #region Install
    public void Install()
    {

      SqlCommand cmd = null;
      SqlConnection conn = null;

      try
      {
        string connString =
          "Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=master;Data Source=.\\SQLExpress";

        conn = new SqlConnection(connString);

        string query = string.Format("sp_attach_db \"GShield\", \"{0}\", \"{1}\"",
            Application.StartupPath + "\\GShield.mdf",
            Application.StartupPath + "\\GShield_log.LDF"
        );
        cmd = new SqlCommand(query, conn);
        cmd.CommandType = CommandType.Text;

        conn.Open();
        cmd.ExecuteNonQuery();
      }
      catch(SqlException se)
      {
        // Eat a SQL Exception as it most likely means that the Db is already installed.
      }
      finally
      {
        if (cmd != null) cmd.Dispose();
        if (conn != null) conn.Dispose();
      }
    }
    #endregion Install

    #region Start Service
    public void StartService()
    {
      try
      {
        ServiceController controller = new ServiceController();
        controller.ServiceName = "MSSQL$SQLEXPRESS";

        controller.Start();
      }
      catch
      {
        // Eat all exceptions...
      }
    }
    #endregion Start Service

		#region Get Service Status
		/// <summary>
		/// Checks to see if the SQL Express Service is running.
		/// </summary>
		/// <returns>True if the service is running, else false.</returns>
		public bool GetServiceStatus()
		{
			try
			{
				ServiceController controller = new ServiceController();
				controller.ServiceName = "MSSQL$SQLEXPRESS";

				if (controller.Status == ServiceControllerStatus.Running)
					return true;
				else
					return false;
			}
			catch
			{
				return false;
			}
		}
		#endregion Get Service Status
	}
}